﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_NewsDAL
    {
        public List<WI_News> GetAllNews(int start, int limit, out int ttlCount, string queryString)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_News.AsQueryable();
                if (!string.IsNullOrWhiteSpace(queryString))
                {
                    query = query.Where(c => c.NewsTitle.Contains(queryString));
                }
                query = query.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.NewsID);
                ttlCount = query.Count();
                List<WI_News> newslist = query.Skip(start).Take(limit).ToList();
                return newslist;
            }
        }

        /// <summary>
        /// 查找新闻标题和副标题
        /// </summary>
        public List<WI_News> SearchNewsByTitle(int start, int limit, out int ttlCount, string newsTitle)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_News.Where(c => c.NewsStatus == 1 && (c.NewsTitle.Contains(newsTitle) || c.SubTitle.Contains(newsTitle)));
                query = query.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.NewsID);
                ttlCount = query.Count();
                List<WI_News> newslist = query.Skip(start).Take(limit).ToList();
                return newslist;
            }
        }

        /// <summary>
        /// 查找新闻内容。
        /// </summary>
        public List<WI_News> SearchNewsByDescr(int start, int limit, out int ttlCount, string newsDescr)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_News.Where(c => c.NewsStatus == 1 && c.Description.Contains(newsDescr));
                query = query.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.NewsID);
                ttlCount = query.Count();
                List<WI_News> newslist = query.Skip(start).Take(limit).ToList();
                return newslist;
            }
        }

        /// <summary>
        /// 获取新闻分页（预编译查询版本）
        /// </summary>
        public List<WI_News> GetAllNews(int start, int limit)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                List<WI_News> newslist = NewsQuery.GetAllNewsQuery(ctx, start, limit).ToList();
                return newslist;
            }
        }

        public List<WI_News> GetAllNews(int cataId, int start, int limit, out int ttlCount, string queryString)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_News.Where(c => c.CataID == cataId);
                if (!string.IsNullOrWhiteSpace(queryString))
                {
                    query = query.Where(c => c.NewsTitle.Contains(queryString));
                }
                query = query.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.NewsID);
                ttlCount = query.Count();
                List<WI_News> newslist = query.Skip(start).Take(limit).ToList();
                return newslist;
            }
        }

        /// <summary>
        /// 分类获取新闻分页（预编译查询版本）
        /// </summary>
        public List<WI_News> GetAllNews(int cataId, int start, int limit)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                List<WI_News> newslist = NewsQuery.GetAllNewsQueryByCata(ctx, cataId, start, limit).ToList();
                return newslist;
            }
        }

        /// <summary>
        /// 根据上级CataID获取新闻分页（预编译查询版本）
        /// </summary>
        public List<WI_News> GetAllNewsByPCataID(int pcataId, int start, int limit)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                var query = NewsQuery.GetAllNewsQueryByParentCata(ctx, pcataId, start, limit);
                List<WI_News> newsList = query.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.NewsID).ToList();
                return newsList;
            }
        }

        public List<WI_News> GetAllNewsByPCataID(int pcataId, int start, int limit, out int ttlCount, string queryString)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                var query = ctx.WI_News.Where(c => c.WI_NewsCata.ParentCataID == pcataId);
                if (!string.IsNullOrWhiteSpace(queryString))
                {
                    query = query.Where(c => c.NewsTitle.Contains(queryString));
                }
                ttlCount = query.Count();
                List<WI_News> newsList = query.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.NewsID)
                    .Skip(start).Take(limit).ToList();
                return newsList;
            }
        }

        public List<WI_News> GetAllNewsByMenuID(int menuId, int start, int limit)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                var query = from nw in ctx.WI_News
                            join mi in ctx.FW_MenuItem on nw.WI_NewsCata.MenuItemID equals mi.ItemID
                            where mi.MenuID == menuId
                            select nw;
                List<WI_News> newsList = query.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.NewsID).Skip(start)
                    .Take(limit).ToList();
                return newsList;
            }
        }

        public WI_News GetNews(int newsId, bool lazyLoading)
        {
            SugardbEntities ctx = new SugardbEntities();
            ctx.ContextOptions.LazyLoadingEnabled = lazyLoading;
            WI_News news = ctx.WI_News.SingleOrDefault(c => c.NewsID == newsId);
            return news;
        }

        public int CreateNews(News news)
        {
            int newsId = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_News winews = new WI_News
                {
                    NewsTitle = news.NewsTitle,
                    SubTitle = news.SubTitle,
                    NewsType = news.NewsType,
                    Keywords = news.Keywords,
                    Description = news.Description,
                    CataID = news.CataID,
                    CreateTime = DateTime.Now,
                    Creator = news.Creator,
                    PubTime = news.PubTime,
                    Publisher = news.Publisher,
                    SortNum = news.SortNum,
                    ViewCount = 0,
                    NewsCover = news.NewsCover,
                    LastUpdated = news.LastUpdated,
                    NewsStatus = news.NewsStatus,
                    NewsFile = news.NewsFile,
                    FileTitle = news.FileTitle,
                    Important = news.Important,
                    NewsSrc = news.NewsSrc
                };
                ctx.WI_News.AddObject(winews);
                ctx.SaveChanges();
                newsId = winews.NewsID;
            }
            return newsId;
        }

        public bool UpdateNews(int newsId, News v_news)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_News winews = ctx.WI_News.SingleOrDefault(c => c.NewsID == newsId);
                if (winews != null)
                {
                    winews.NewsTitle = v_news.NewsTitle;
                    winews.SubTitle = v_news.SubTitle;
                    winews.NewsType = v_news.NewsType;
                    winews.Keywords = v_news.Keywords;
                    winews.Description = v_news.Description;
                    winews.CataID = v_news.CataID;
                    winews.PubTime = v_news.PubTime;
                    winews.Publisher = v_news.Publisher;
                    winews.SortNum = v_news.SortNum;
                    winews.NewsCover = v_news.NewsCover;
                    winews.LastUpdated = DateTime.Now;
                    winews.NewsStatus = v_news.NewsStatus;
                    winews.NewsFile = v_news.NewsFile;
                    winews.FileTitle = v_news.FileTitle;
                    winews.Important = v_news.Important;
                    winews.NewsSrc = v_news.NewsSrc;
                    ctx.SaveChanges();
                }
            }
            return result;
        }

        public bool UpdateViewCount(int newsId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_News winews = ctx.WI_News.FirstOrDefault(c => c.NewsID == newsId);
                if (winews != null)
                {
                    int vcount = winews.ViewCount ?? 0;
                    vcount++;
                    winews.ViewCount = vcount;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveNews(int newsId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_News news = ctx.WI_News.SingleOrDefault(c => c.NewsID == newsId);
                if (news != null)
                {
                    ctx.WI_News.DeleteObject(news);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
